#include "NotifyQueue.h"
#include "log_err.h"
#include <unistd.h>

NotifyQueue* NotifyQueue::instance = new NotifyQueue();

NotifyQueue* NotifyQueue::getInstance() {
    return instance;
}

NotifyQueue::NotifyQueue(/* args */)
{
}

NotifyQueue::~NotifyQueue()
{
    clear();
}

void NotifyQueue::clear() {
    SIGNWAY_LOGI("clear");
    while (!notifyQueue.empty()){
        notifyQueue.pop();
    }
    SIGNWAY_LOGI("clear");
}

void NotifyQueue::threadMain()
{
    Notify* notify;
    threadIsStarted = true;
    while (true)
    {
        usleep(1000);

        mutex.lock();

        if (notifyQueue.empty()) {
            mutex.unlock();continue;
        }

        if (notifyQueue.size() >= 255) {
            clear();/* 存储过多，清空 */
            mutex.unlock();continue;
        }

        /* 发送通知 */
        auto notify = notifyQueue.front();
        notifyQueue.pop();
        SIGNWAY_LOGI("%d", notify->notifyId);
        // if (notify->notifyId < 10000) {
        //     // if ((true) || (BaseActivity::allowRefreshInterface)) {
        //         // actManager->sendNotify(notify->notifyId, notify->data);
        //     // }
        // }

NotifyQueueErr:
        mutex.unlock();
    }
}

void NotifyQueue::sendNotify(int notifyId, void* data)
{
    mutex.lock();
    if (notifyQueue.size() >= 255) {
        clear();/* 存储过多，清空 */
    }
    // if (Control::startHomeActivity) {
        notifyQueue.push(std::shared_ptr<Notify>(new Notify((int)notifyId, data)));
    // }
    mutex.unlock();
}

